(function() {
	"use strict";
	var pi = Math.PI;

	var secondCanvas = null;
	var minuteCanvas = null;
	var hourCanvas = null;

	var secondr = 48;
	var minr = 45;
	var hourr = 35;

	var circleX = 70;
	var circleY = 72;
	var clock_body = "clock_body";
	
	var id = 0;

	function XClock(parent) {
		if (!parent) parent = $(document.body);
		this.parent = parent;
		this.dom = $("<div>").addClass(clock_body).appendTo(parent);
		this.showTimeFuncs = {};
		this.refreshTime();
		setTime.call(this);
		setInterval(setTime.bind(this), 1000);

	}
	function createSecondHand(seconds, offset) {
		if (secondCanvas) {
			$(secondCanvas).remove();
			secondCanvas = null;
		}
		var angle = seconds * pi / 30;
		var curY = circleY - secondr * Math.cos(angle);
		var curX = secondr * Math.sin(angle) + circleX;
		secondCanvas = crateCanvas.call(this, curX, curY, offset, 1);

	}

	function createMinuteHand(mins, offset) {
		if (minuteCanvas) {
			$(minuteCanvas).remove();
			minuteCanvas = null;
		}
		var angle = mins * pi / 30;
		var curY = circleY - minr * Math.cos(angle);
		var curX = minr * Math.sin(angle) + circleX;
		minuteCanvas = crateCanvas.call(this, curX, curY, offset, 2);
	}
	function createHourHand(hours, offset) {
		if (hourCanvas) {
			$(hourCanvas).remove();
			hourCanvas = null;
		}
		var angle = hours * pi / 6 + (this.mins / 60) * (pi / 6);
		var curY = circleY - hourr * Math.cos(angle);
		var curX = hourr * Math.sin(angle) + circleX;
		hourCanvas = crateCanvas.call(this, curX, curY, offset, 2);
	}

	function crateCanvas(x, y, offset, lineWidth) {
		var canvas = document.createElement("canvas");
		$(canvas).css({
			position : "absolute",
		});
		this.dom[0].appendChild(canvas);
		var ctx = canvas.getContext('2d');
		ctx.fillStyle = 'red';
		ctx.lineWidth = lineWidth;
		ctx.beginPath();
		ctx.moveTo(circleX, circleY);
		ctx.lineTo(x, y);
		ctx.strokeStyle = 'rgb(84,103,111)';
		ctx.stroke();
		return canvas;
	}
	/**
	 * 自动执行的方法
	 */
	function setTime() {
		var date = this.date = new Date();
		var hours = this.hours = date.getHours();
		var seconds = this.seconds = date.getSeconds();
		var mins = this.mins = date.getMinutes();
		var offset = this.dom.offset();
		createSecondHand.call(this, seconds - this.secondgap, offset);
		createMinuteHand.call(this, mins - this.mingap, offset);
		createHourHand.call(this, hours - this.hourgap, offset);
		for(var i in this.showTimeFuncs){
			this.showTimeFuncs[i].call(this);
		}

	}
	
	/**
	 * 设置时钟位置
	 * @param x
	 * @param y
	 */
	XClock.prototype.setPosition = function(x, y) {
		this.dom.css({
			left : x + "px",
			top : y + "px"
		});
	};
	/**
	 * 设置时间
	 * 
	 * @param hour
	 * @param min
	 * @param second
	 */
	XClock.prototype.setTime = function(hour, min, second) {
		if (hour && hour >= 0 && hour <= 23) {
			this.hourgap = this.hours - hour;
		}
		if (min && min >= 0 && min <= 59) {
			this.mingap = this.mins - min;
		}
		if (second && second >= 0 && second <= 59) {
			this.secondgap = this.seconds - second;
		}
		setTime.call(this);

	};
	
	/**
	 * 更正为正确时间
	 */
	XClock.prototype.refreshTime = function(){
		this.hourgap = 0 ;
		this.mingap = 0;
		this.secondgap = 0;
		setTime.call(this);
	};
	
	/**
	 * 自己提供一个接口，方便别处显示时钟时间
	 * 返回一个id，这个id可以找到那个方法，主要的作用是移除方法
	 * 方法里面this对象就是clock ，this.date就可以自定义输出
	 * @param func
	 */
	XClock.prototype.setShowTimeFunc = function(func,funcid){
		if(!funcid)
			funcid = id ;
			this.showTimeFuncs[funcid] = func;
			id++;
			return funcid;
	};
	
	window["XClock"] = XClock;
})();
